﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace Agresso.Interface.Layer.Service
{
    public class EmailService
    {
        public static void SendErrorEmail(string message, string to)
        {
            MailAddress SendFrom = new MailAddress("DEV01@kuokgroup.com.sg", "DEVELOPMENT SUPPORT");//carina@kuokgroup.com.sg
            MailMessage MyMessage = new MailMessage();
            MyMessage.From = SendFrom;
            MyMessage.To.Add(to);
            
            //string host = System.Configuration.ConfigurationSettings.AppSettings["DWSEAHT02.DWSEA.RIG"];
            SmtpClient emailClient = new SmtpClient("smtp.gmail.com", 587);//kuoksmtp01.kuokgroup.com.sg - DWSEAHT01.DWSEA.RIG
            emailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            emailClient.Credentials = new NetworkCredential("dagado", "dagadotri");
            emailClient.EnableSsl = true;
            emailClient.UseDefaultCredentials = false;
            emailClient.Send(MyMessage);
        }

        public static bool SendByGmail(string subject, string content, string[] recipients)
        {
            bool success = recipients != null && recipients.Length > 0;
            if (success)
            {
                SmtpClient gmailClient = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    UseDefaultCredentials = false,
                    Credentials = new System.Net.NetworkCredential("development.kuokgroup", "D3v3lopm3nt")
                };
                using (MailMessage gMessage = new MailMessage())
                {
                    MailAddress SendFrom = new MailAddress("DEV01@kuokgroup.com.sg", "DEVELOPMENT SUPPORT");
                    gMessage.From = SendFrom;
                    gMessage.Subject = subject;
                    gMessage.Body = content;
                    for (int i = 0; i < recipients.Length; i++)
                        gMessage.To.Add(recipients[i]);
                    try
                    {
                        gmailClient.Send(gMessage);
                        success = true;
                    }
                    catch (Exception) { success = false; }
                }
            }
            return success;
        }

        public static bool SendByLocalServer(string subject, string content, string[] recipients)
        {
            bool success = recipients != null && recipients.Length > 0;
            if (success)
            {
                SmtpClient localClient = new SmtpClient
                {
                    Host = "172.26.10.12",
                    UseDefaultCredentials = true
                };
                using (MailMessage gMessage = new MailMessage())
                {
                    MailAddress SendFrom = new MailAddress("DEV01@kuokgroup.com.sg", "DEVELOPMENT SUPPORT");
                    gMessage.From = SendFrom;
                    gMessage.Subject = subject;
                    gMessage.Body = content;
                    for (int i = 0; i < recipients.Length; i++)
                        gMessage.To.Add(recipients[i]);
                    try
                    {
                        localClient.Send(gMessage);
                        success = true;
                    }
                    catch (Exception x) { success = false; }
                }
            }
            return success;
        }

        public static bool SendByLocalServer(string subject, string content, string recipient)
        {
            bool success = recipient != null && recipient.Length > 0;
            if (success)
            {
                SmtpClient localClient = new SmtpClient
                {
                    Host = "172.26.10.12",
                    UseDefaultCredentials = true
                };
                using (MailMessage gMessage = new MailMessage())
                {
                    MailAddress SendFrom = new MailAddress("DEV01@kuokgroup.com.sg", "IT Interface");
                    gMessage.From = SendFrom;
                    gMessage.Subject = subject;
                    gMessage.Body = content;
                    gMessage.IsBodyHtml = true;
                    gMessage.To.Add(recipient);
                    try
                    {
                        localClient.Send(gMessage);
                        success = true;
                    }
                    catch (Exception x) { success = false; }
                }
            }
            return success;
        }

        public static bool SendFatalErrorToDeveloper(string subject, string content, string recipient)
        {
            bool success = recipient != null && recipient.Length > 0;
            if (success)
            {
                SmtpClient localClient = new SmtpClient
                {
                    Host = "172.26.10.12",
                    UseDefaultCredentials = true
                };
                using (MailMessage gMessage = new MailMessage())
                {
                    MailAddress SendFrom = new MailAddress("DEV01@kuokgroup.com.sg", "IT Interface");
                    gMessage.From = SendFrom;
                    gMessage.Subject = subject;
                    gMessage.Body = content;
                    gMessage.IsBodyHtml = true;
                    gMessage.To.Add(recipient);
                    try
                    {
                        localClient.Send(gMessage);
                        success = true;
                    }
                    catch (Exception x) { success = false; }
                }
            }
            return success;
        }

        public static bool SendByDEVServer(string subject, string content, string recipient)
        {
            bool success = recipient != null && recipient.Length > 0;
            if (success)
            {
                SmtpClient localClient = new SmtpClient
                {
                    Host = "localhost",
                    UseDefaultCredentials = true
                };
                using (MailMessage gMessage = new MailMessage())
                {
                    MailAddress SendFrom = new MailAddress("DEV01@kuokgroup.com.sg", "IT Interface");
                    gMessage.From = SendFrom;
                    gMessage.Subject = subject;
                    gMessage.Body = content;
                    gMessage.IsBodyHtml = true;
                    gMessage.To.Add(recipient);
                    try
                    {
                        localClient.Send(gMessage);
                        success = true;
                    }
                    catch (Exception x) { success = false; }
                }
            }
            return success;
        }
    }

}
